<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default.html(bodyClass='pear-container')}">
<head>
    <title>表信息管理</title>
</head>
<body>
<section layout:fragment="wrapper">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">数据源</label>
                        <div class="layui-input-inline">
                            <select name="sourceId" class="layui-select" lay-search>
                                <option value=""></option>
                                <option th:each="dataSource:${dataSources}"
                                        th:value="${dataSource.id}"
                                        th:text="${dataSource.name}"></option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">表名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="tableName" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="tb-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="tb-table" lay-filter="tb-table"></table>
        </div>
    </div>

    <script type="text/html" id="tb-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>

    <script type="text/html" id="tb-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </script>

    <script th:inline="javascript">
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            table.render({
                elem: '#tb-table',
                url: '[(@{/genTableInfo/page})]',
                method: 'post',
                response: {
                    statusCode: '20000',
                },
                page: true,
                cols: [
                    [
                        {type: 'checkbox'},
                        {title: '数据源', field: 'sourceName', align: 'center'},
                        {title: '名称', field: 'name', align: 'center'},
                        {title: '表名', field: 'tableName', align: 'center'},
                        {title: '说明', field: 'tableComment', align: 'center'},
                        {title: '实体名称', field: 'entityName', align: 'center', width: 100},
                        {title: '包名', field: 'packageName', align: 'center'},
                        {title: '排序', field: 'sort', align: 'center', width: 80},
                        {title: '操作', toolbar: '#tb-bar', align: 'center', width: 160}
                    ]
                ],
                skin: 'line',
                toolbar: '#tb-toolbar',
                defaultToolbar: [
                    {title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh'},
                    'filter', 'exports', 'print'
                ]
            });

            table.on('tool(tb-table)', function (obj) {
                if (obj.event === 'remove') {
                    remove(obj);
                } else if (obj.event === 'edit') {
                    edit(obj);
                }
            });

            table.on('toolbar(tb-table)', function (obj) {
                if (obj.event === 'add') {
                    add();
                } else if (obj.event === 'refresh') {
                    refresh();
                } else if (obj.event === 'batchRemove') {
                    batchRemove(obj);
                }
            });

            form.on('submit(tb-query)', function (data) {
                table.reload('tb-table', {
                    where: data.field,
                    page: {
                        curr: 1
                    }
                })
                return false;
            });

            const add = function () {
                // parent.layui.admin.addTab('genTableInfo_form', "表新增", '[(@{/genTableInfo/form})]');

                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/genTableInfo/add})]'
                });
            }

            const edit = function (obj) {
                // parent.layui.admin.addTab('genTableInfo_form', "表修改", '[(@{/genTableInfo/form})]?id=' + obj.data['id']);

                let index = layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/genTableInfo/form})]?id=' + obj.data['id']
                });
                layer.full(index);
            }

            const remove = function (obj) {
                removeData([obj.data['id']]);
            }

            const batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                removeData(ids);
            }

            const removeData = function (array) {
                layer.confirm('确定要删除数据', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: '[(@{/genTableInfo/del})]',
                        data: {datas: array},
                        dataType: 'json',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code === '20000') {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    refresh()
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            const refresh = function () {
                table.reload('tb-table');
            }
        })
    </script>
</section>
</body>
</html>
